package com.beiing.tianshuai.tianshuai.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.app.HttpRequestConstant;
import com.beiing.tianshuai.tianshuai.base.BaseRecyclerViewAdapter;
import com.beiing.tianshuai.tianshuai.entity.MyCollectionBean;
import com.beiing.tianshuai.tianshuai.util.TimeUtils;
import com.beiing.tianshuai.tianshuai.widget.CircleImageView;
import com.beiing.tianshuai.tianshuai.widget.FooterView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;

import java.text.ParseException;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bingoogolapple.bgabanner.BGAOnNoDoubleClickListener;

/**
 * Created by Jinni on 2017/7/30 0030.
 */

public class PersonalCollectionAdapter extends BaseRecyclerViewAdapter<MyCollectionBean.DataBean.NumberBean> {

    private static final int DYNAMIC_NORMAL = 0;                 // 普通动态
    private static final int ITEM_VIEW_TYPE_FOOTER = 2;          // 转发动态

    private Context             mContext;
    private int                 mFooterStatus;
    private OnItemClickListener mListener;

    public void setListener(OnItemClickListener listener) {
        mListener = listener;
    }

    public PersonalCollectionAdapter(Context context) {
        this.mContext = context;
    }

    public void setFooterStatus(int footerStatus) {
        mFooterStatus = footerStatus;
    }

    @Override
    public int getItemViewType(int position) {
        int result;
        if (position == mList.size()) {
            result = ITEM_VIEW_TYPE_FOOTER;
        } else {
            result = DYNAMIC_NORMAL;
        }
        return result;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        switch (viewType) {
            case DYNAMIC_NORMAL:
                holder = new ViewHolder(View.inflate(mContext, R.layout.item_personal_collection, null));
                break;
            case ITEM_VIEW_TYPE_FOOTER:
                if (mFooterView == null) {
                    mFooterView = new FooterView(mContext);
                    mFooterView.setLayoutParams(
                            new LinearLayout.LayoutParams(
                                    ViewGroup.LayoutParams.MATCH_PARENT,
                                    ViewGroup.LayoutParams.WRAP_CONTENT
                            )
                    );
                }
                return new FooterViewHolder(mFooterView);
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            final MyCollectionBean.DataBean.NumberBean bean = mList.get(position);
            RequestOptions requestOptions = new RequestOptions() // 头像Glide处理方法
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .centerCrop()
                    .override(150, 150);
            RequestOptions aPicRequestOptions = new RequestOptions() // 活动图片处理方法
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .centerCrop()
                    .override(500, 280);
            Glide.with(mContext.getApplicationContext())
                    .load(TextUtils.isEmpty(
                            bean.getPhoto()) ?
                            R.mipmap.default_avatar :
                            HttpRequestConstant.ACTIVITY_PIC_HEAD + bean.getCover())
                    .apply(aPicRequestOptions)
                    .into(((ViewHolder) holder).mIvCollectionCover);
            Glide.with(mContext.getApplicationContext())
                    .load(TextUtils.isEmpty(
                            bean.getPhoto()) ?
                            R.mipmap.default_avatar :
                            HttpRequestConstant.USER_PIC_HEAD + bean.getPhoto())
                    .apply(requestOptions)
                    .into(((ViewHolder) holder).mCivUserAvatar);
            ((ViewHolder) holder).mTxtCollectionTitle.setText(bean.getStrech());
            ((ViewHolder) holder).mTxtUserName.setText(bean.getUname());
            try {
                String timeEnd = TimeUtils.longToString(Long.parseLong(bean.getExitTime()), "yyyy.MM.dd");
                ((ViewHolder) holder).mTxtCollectionTime.setText(timeEnd);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            ((ViewHolder) holder).mItem.setOnClickListener(new BGAOnNoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    mListener.onItemClickListener(holder.getAdapterPosition(), bean);
                }
            });
        } else if (holder instanceof FooterViewHolder) {
            if (position == mList.size() && mFooterView.getStatus() != FooterView.NO_MORE) {
                setFooterViewStatus(mFooterStatus);
            }
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private View mItem;

        @BindView(R.id.iv_collection_cover)
        ImageView mIvCollectionCover;
        @BindView(R.id.txt_collection_title)
        TextView mTxtCollectionTitle;
        @BindView(R.id.civ_user_avatar)
        CircleImageView mCivUserAvatar;
        @BindView(R.id.txt_user_name)
        TextView mTxtUserName;
        @BindView(R.id.txt_collection_time)
        TextView mTxtCollectionTime;
        @BindView(R.id.civ_collection_type)
        CircleImageView mCivCollectionType;

        ViewHolder(View view) {
            super(view);
            mItem = view;
            ButterKnife.bind(this, view);
        }
    }

    @Override
    public int getItemCount() {
        if (mList != null && !mList.isEmpty()) {
            return mList.size();
        } else {
            return 0;
        }
    }

    public interface OnItemClickListener{
        void onItemClickListener(int position,MyCollectionBean.DataBean.NumberBean bean);
    }
}
